package com.sinvo.oa.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinvo.oa.entity.model.process.ProcessType;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.log.annotation.Log;
import com.sinvo.oa.log.enums.ServiceType;
import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.ProcessTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 审批类型控制器
 */
@Api(tags = "审批类型管理接口")
@RestController
@RequestMapping("admin/process/processType")
public class ProcessTypeController {

    @Autowired
    private ProcessTypeService processTypeService;

    /**
     * 获取所有分类
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.processType.list')")
    @ApiOperation(value = "获取所有审批分类")
    @GetMapping("findAll")
    public R findAll(){
        List<ProcessType> typeList = processTypeService.list();
        return R.success(typeList);
    }

    /**
     * 分页获取
     *
     * @param pageNum  当前页
     * @param pageSize 页大小
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.processType.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{pageNum}/{pageSize}")
    public R page(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        //封装分页参数
        Page<ProcessType> page = new Page<>(pageNum, pageSize);
        //分页查询
        page = processTypeService.page(page);
        return R.success(page);
    }

    /**
     * 获取单个类型
     * @param id typeId
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.processType.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public R get(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        ProcessType processType = processTypeService.getById(id);
        return R.success(processType);
    }

    /**
     * 新增审批类型
     * @param processType 类型
     * @param result 保存错误信息
     * @return R
     */
    @Log(title = "审批类型管理",businessType = ServiceType.INSERT)
    @PreAuthorize("hasAuthority('btn.processType.add')")
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public R save(@Validated @RequestBody ProcessType processType, BindingResult result) {
        //参数校验
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        //保存审批类型
        processTypeService.saveType(processType);
        return R.success();
    }

    /**
     * 更新审批类型
     * @param processType 类型
     * @param result 保存错误信息
     * @return R
     */
    @Log(title = "审批类型管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.processType.update')")
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public R update(@Validated @RequestBody ProcessType processType,BindingResult result) {
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        processTypeService.updateType(processType);
        return R.success();
    }

    /**
     * 删除一个模板
     * @param id typeId
     * @return R
     */
    @Log(title = "审批类型管理",businessType = ServiceType.DELETE)
    @PreAuthorize("hasAuthority('btn.processType.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        boolean success = processTypeService.removeById(id);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "删除失败，请重试");
        }
        return R.success();
    }
}

